//
//  ViewController.m
//  CommunityPrj
//
//  Created by apple on 2017/9/1.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "ViewController.h"
#import "BXGCommentViewModel.h"
#import "BXGCommentCell.h"
#import "BXGCommentDataModel.h"
#import "BXGDataFactory.h"
#import "BXGCommentReplyViewModel.h"

NSInteger g_commentId = 1000;

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong) NSMutableDictionary<NSString*/*commentId*/, BXGCommentViewModel*> *dictCommentViewModel;
@property(nonatomic, strong) NSMutableArray<BXGCommentDataModel*> *arrCommentDataModel;
@property(nonatomic, strong) UITableView *tableView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _arrCommentDataModel = [[BXGDataFactory shareInstance] buildCommentDataModel];
    _dictCommentViewModel = [NSMutableDictionary new];
    
    _tableView = [[UITableView alloc] init];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.tableFooterView = [UIView new];
    _tableView.backgroundColor =  [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0)
                                                  green:((float)arc4random_uniform(256) / 255.0)
                                                   blue:((float)arc4random_uniform(256) / 255.0)
                                                  alpha:1.0];
    [self.view addSubview:_tableView];
    _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    
    [_tableView registerNib:[UINib nibWithNibName:@"BXGCommentCell" bundle:nil]
     forCellReuseIdentifier:@"BXGCommentCell"];
    
    [_tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrCommentDataModel.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(!_arrCommentDataModel || indexPath.row>=_arrCommentDataModel.count)
        return nil;

    BXGCommentViewModel *commentViewModel = [self curRenderCellViewModelAtIndex:indexPath.row];
    if(!commentViewModel)
    {
        return nil;
    }
    BXGCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BXGCommentCell" forIndexPath:indexPath];
    cell.index = indexPath.row;
    [cell setupCell:commentViewModel];
    cell.sendBlock = ^(NSInteger index) {
        [self addComment:index];
    };
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BXGCommentViewModel *commentViewModel = [self curRenderCellViewModelAtIndex:indexPath.row];
    if(!commentViewModel)
    {
        return 0;
    }
    return commentViewModel.cellHeight;
}

-(void)addComment:(NSInteger)index
{
    if(!_arrCommentDataModel || index>=_arrCommentDataModel.count)
        return ;
    
    //准备添加的素材资源
    g_commentId++;
    BXGCommentDataModel *model1 = [BXGCommentDataModel new];
    model1.commentId = [NSString stringWithFormat:@"%ld", g_commentId];
    model1.text = @"新增记录";
    model1.arrMediaImage = nil;
    
    //找到待添加的元素
    BXGCommentDataModel *commentDataModel = _arrCommentDataModel[index];
    if(!commentDataModel.arrReplyContent)
    {
        commentDataModel.arrReplyContent = [NSMutableArray new];
    }
    [commentDataModel.arrReplyContent addObject:model1];

    //如果字典里面有保存的信息，需要删除。
    BXGCommentViewModel *commentVM = [_dictCommentViewModel objectForKey:commentDataModel.commentId];
    if(commentVM)
    {
        [_dictCommentViewModel removeObjectForKey:commentDataModel.commentId];
    }
    
    //刷新界面
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    NSLog(@"row=%ld", indexPath.row);
    //    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [self.tableView reloadData];//不能带动子控件刷新
    
}

-(BXGCommentViewModel*)curRenderCellViewModelAtIndex:(NSInteger)index
{
    if(!_arrCommentDataModel || index>=_arrCommentDataModel.count)
        return nil;

    BXGCommentDataModel *commentDataModel = _arrCommentDataModel[index];
    BXGCommentViewModel *commentVM = [_dictCommentViewModel objectForKey:commentDataModel.commentId];
    if(!commentVM)
    {
        commentVM = [[BXGCommentViewModel alloc] init];
        commentVM.dataModel = commentDataModel;
        [_dictCommentViewModel setObject:commentVM forKey:commentDataModel.commentId];
    }
    return commentVM;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
